/*
 * p2148.cpp
 *
 *  Created on: 2013-3-14
 *      Author: zy
 */

#include<cstdio>
#include<algorithm>
using namespace std;
const int INF=0x7ffffff;
const int maxn=100001;
struct node
{
	int w,v;
	bool friend operator <(node a,node b)
	{
		return a.w>b.w;
	}
}a[maxn];
int dp[2][maxn];
int M,N;

int main()
{
	while(~scanf("%d",&N))
	{
		M=0;
		for(int i=1;i<=N;i++)
		{
			scanf("%d%d",&a[i].w,&a[i].v);
			if(a[i].w>0)M+=a[i].w;
		}
		sort(a+1,a+N+1);
		for(int i=1;i<maxn;i++)
			dp[0][i]=-INF;
		dp[0][0]=0;
		for(int i=1;i<=N;i++)
			for(int j=0;j<=M;j++)
				if(j>=a[i].w&&j-a[i].w<=M&&dp[(i-1)%2][j-a[i].w]+a[i].v>dp[(i-1)%2][j])
					dp[i%2][j]=dp[(i-1)%2][j-a[i].w]+a[i].v;
				else
					dp[i%2][j]=dp[(i-1)%2][j];
		int ans=0;
		for(int i=0;i<=M;i++)
			if(dp[N%2][i]>=0&&i+dp[N%2][i]>ans)ans=i+dp[N%2][i];
		printf("%d\n",ans);
	}
	return 0;
}
